package common.fengkai.filepost.utils;

import java.io.File;
import java.util.Arrays;


import cfca.paperless.client.servlet.PaperlessClient;

import cfca.paperless.util.GUID;
import cfca.paperless.util.IoUtil;
import cfca.paperless.util.StringUtil;
import common.fengkai.filepost.conf.PaperlessConfig;
import org.apache.commons.io.FileUtils;

/**
 * @Description Pdf验章接口，使用示例。 
 * 1，操作对象为本地待验章的单个PDF文件。 
 * 2，返回验章结果。
 * 
 * @Author dell
 * @Date 2016-5-13
 * @CodeReviewer TODO
 */
public class VerifyPdfSignatureTest01 {

    public static void main(String[] args) throws Exception {

        long timeStart = System.currentTimeMillis();// 开始时间

        System.out.println("VerifyPdfSignatureTest01 START");

        String fileTobytes = Base64util.encodeBase64File("C:\\Users\\carry\\Desktop\\电子保单测试\\更新后signed55.pdf");
        byte[] tobytes = Base64util.decoderBase64ToByte(fileTobytes);
        byte[] pdfFile2 = FileUtils.readFileToByteArray(new File("C:\\Users\\carry\\Desktop\\电子保单测试\\更新后signed55.pdf"));

        PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url, 10000, 60000);
        System.out.println("---------------------------------");
        System.out.println(tobytes.length +"== "+pdfFile2.length);
        String systemNo = GUID.generateId() ;// 业务流水号 


        String verifyStrategyXml = "<Request><SealVerifyType>0</SealVerifyType></Request>"; // 验章策略文件

        String result = clientBean.verifyPdfSignature(systemNo, tobytes, verifyStrategyXml, PaperlessConfig.operatorCode, PaperlessConfig.channelCode);

        String code = StringUtil.getXmlField(result, "Code");

        // 错误码（Code不等于200时为异常）
        if ("200".equals(code)) {
            System.out.println("VerifyPdfSignatureTest01 END OK");
            System.out.println(result);
            int esealtitleStartIndex = result.indexOf("<Dn>")+4;
            int esealtitleEndIndex = result.indexOf("</Dn>");

            String esealtitle = result.substring(esealtitleStartIndex, esealtitleEndIndex);
            System.out.println("证书主题========"+esealtitle);

            int serialNoStartIndex = result.indexOf("<SerialNo>")+10;
            int serialNoEndIndex = result.indexOf("</SerialNo>");
            String serialNo = result.substring(serialNoStartIndex, serialNoEndIndex);
            System.out.println("证书编号========"+serialNo);

            int orgNoStartIndex = result.indexOf("<IssuerDn>")+10;
            int orgNoEndIndex = result.indexOf("</IssuerDn>");
            String orgName = result.substring(orgNoStartIndex, orgNoEndIndex);
            System.out.println("证书机构========"+orgName);


        } else {
            // 处理结果为异常，打印出错误码和错误信息
            System.out.println("VerifyPdfSignatureTest01 END NG");
            System.out.println(result);

        }
        
        long timeEnd = System.currentTimeMillis();// 结束时间
        System.out.println("##########" + "time used:" + (timeEnd - timeStart) + "##########");

    }

}
